const sensitiveWords = require('yi-sensitive-words');
module.exports = {
  open:false,
  description:'新增评论',
  handler(req,res,next){
    let doc = req.body;
    let id = doc.cardID;
    let content = doc.content;
    let commenterID = doc.commenterID;
    doc.createTime = new Date();
    sensitiveWords(content,function(r){
      if(r.length >0){
        return res.reply({"code":1500,"errMsg":"评论发送失败！评论内容含有敏感词："+r[0][1]})
      }

      req.mongo.collection("user").find({"_id":commenterID}).next((e,r)=>{
        if(e) return res.err.mongoError(e);
        if(r.commentState==1){//如果有评论权限
          req.mongo.collection("comment").insertOne(doc,(e,r)=>{
            if(e) return res.err.mongoError(e);
            let commentID = r.insertedId;
            req.mongo.collection("card").updateOne(
                {"_id":id},
                {$push:{"commentID":commentID}},
                {upsert:true},
                (e,r)=>{
                  if(e) res.err.mongoError(e);
                  let obj = new Object();
                  obj._id = commentID;//将返回值中的insertedId属性赋给obj中新添加的属性_id
                  return res.reply(obj)
                }
            )
          })
        }else if(r.commentState==0){//如果没有评论权限
          return res.reply({"code":1300,"msg":"对不起！您没有权限进行评论！"});
        }
      })



    })
  }
};